package com.pactera.asmp.server.common.logprint.utils;


import com.pactera.asmp.server.common.constants.MagicNumberConstant;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class StringUtils {

    public static final String EMPTY = "";
    public static final String SPACE = " ";
    public static final String DASH = "-";
    public static final String POINT = ".";
    public static final String NULL = "null";
    public static final String LEFT = "[";
    public static final String RIGHT = "]";
    public static final String PREFIX = "[ASMP SERVER] ";

    public static final String UTF_8 = "UTF-8";


    /**
     * 字符串转整数
     *
     * @param str
     * @param defValue
     * @return
     */
    public static int toInt(String str, int defValue) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
        }
        return defValue;
    }

    /**
     * 对象转整数
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static int toInt(Object obj, int defValue) {
        if (obj == null)
            return defValue;
        return toInt(obj.toString(), defValue);
    }

    public static String toString(Object obj, String defString) {
        if (obj == null)
            return defString;
        return obj.toString();
    }

    public static String toString(String str, String defString) {
        if (str == null || str.equals(""))
            return defString;
        return str;
    }

    public static String toString(byte[] data, String charsetName) {
        String resultString = EMPTY;
        try {
            resultString = new String(data, charsetName);
        } catch (UnsupportedEncodingException e) {
            LogUtil.e(append("StringUtils toString error mValue is ", Arrays.toString(data)), e);
        } finally {

        }
        return resultString;
    }

    /**
     * String append
     *
     * @param obj
     * @return
     */
    public static String append(Object... obj) {
        if (CollectionUtils.isArrayNotEmpty(obj)) {
            StringBuffer buf = new StringBuffer();
            for (Object element : obj) {
                if (element != null) {
                    buf.append(element.toString());
                } else {
                    buf.append(EMPTY);
                }
            }
            return buf.toString();
        } else {
            return EMPTY;
        }
    }

    /**
     * 对象转整数
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static long toLong(String obj, long defValue) {
        try {
            return Long.parseLong(obj);
        } catch (Exception e) {
        }
        return defValue;
    }

    /**
     * 字符串转布尔值
     *
     * @param b
     * @return 转换异常返回 false
     */
    public static boolean toBoolean(String b, boolean defValue) {
        try {
            return Boolean.parseBoolean(b);
        } catch (Exception e) {
        }
        return defValue;
    }

    /**
     * 截取字符串过多的部分用...来标示
     *
     * @param str
     * @param len
     * @return
     */
    public static String splitString(String str, int len) {
        if (str == null || str.equals("") || len == 0) {
            return "";
        }
        if (str.length() <= len) {
            return str;
        }
        String result = str.substring(0, len);
        return result + "...";
    }


    /**
     * To intercept too many strings
     *
     * @param string
     * @param length
     * @return
     */
    public static String splitStringLength(String string, int length) {
        if (string == null || string.equals("")|| length == MagicNumberConstant.ZERO) {
            return EMPTY;
        }
        if (string.length() <= length) {
            return string;
        }
        String result = string.substring(MagicNumberConstant.ZERO, length);
        return result;
    }

    /**
     * 字符串比较
     *
     * @param str1
     * @param str2
     * @return
     */
    public static boolean equals(String str1, String str2) {
        return (str1 == null ? str2 == null : str1.equals(str2));
    }

    /**
     * 字符串去空格
     *
     * @param str
     * @return
     */
    public static String trimToEmpty(final String str) {
        return str == null ? EMPTY : str.trim();
    }


    /**
     * check string is empty
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * check string not empty
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str) {
        return str != null && str.trim().length() > 0;
    }
}
